<template>
	<el-drawer :title="titleMap[mode]" v-model="visible" :size="1400" destroy-on-close @closed="$emit('closed')">
		<el-container v-loading="loading">
			<el-main style="padding:0 20px 20px 20px">
				<el-form ref="ruleForm" :model="form" :rules="rules" label-width="100px">
					<el-form-item label="素材类目" prop="category_id">
						<blCategory style="width: 400px;" v-model="form.category_id" model="Media" />
					</el-form-item>
					<el-form-item label="文件名" prop="product_title">
						<el-input style="width: 400px;" v-model="form.title"></el-input>
					</el-form-item>
					<template v-if="form.type=='image'">
						<el-form-item label="封面图片">
							<el-image style="max-width: 250px; max-height: 250px;" preview-teleported
								hide-on-click-modal :src="form.src+'?x-oss-process=image/resize,w_250&t='"
								:preview-src-list="[form.src+'?x-oss-process=image/resize,w_250&t=']" show-progress
								fit="contain" :z-index="19900501" :alt="form.title" />
						</el-form-item>
					</template>

					<template v-if="form.type=='video'">
						<el-form-item label="封面图片">
							<el-image style="max-width: 250px; max-height: 250px;" preview-teleported
								hide-on-click-modal :src="form.src+'?x-oss-process=video/snapshot,t_0,f_jpg'"
								:preview-src-list="[form.src+'?x-oss-process=video/snapshot,t_0,f_jpg']" show-progress
								fit="contain" :z-index="19900501" :alt="form.title" />
						</el-form-item>
						<el-form-item label="视频预览">

							<template v-if="form.width<=form.height">
								<div style="width:375px;height: 667px;">
									<sc-video v-if="form.src" :src="form.src" :options="{
										poster: form.src+'?x-oss-process=video/snapshot,t_0,f_jpg',
										pip: true,
										height: 667,
										width: 375,
										}">
									</sc-video>
								</div>
							</template>

							<template v-else>
								<div style="width:667px;height: 375px;">
									<sc-video v-if="form.src" :src="form.src" :options="{
										poster: form.src+'?x-oss-process=video/snapshot,t_0,f_jpg',
										pip: true,
										height: 375,
										width: 667,
										}">
									</sc-video>
								</div>
							</template>
						</el-form-item>
					</template>

					<el-form-item label="文件信息">
						<template v-if="form.type=='image'">
							<el-descriptions :column="1" border size="small">
								<el-descriptions-item label="图片格式">{{form.file_format}}</el-descriptions-item>
								<el-descriptions-item label="图片宽高">{{form.width}} (宽) x {{form.height}}
									(高)</el-descriptions-item>
								<el-descriptions-item label="图片大小">{{(form.file_size/1000).toFixed(2)}}
									KB</el-descriptions-item>
							</el-descriptions>
						</template>
						<template v-if="form.type=='video'">
							<el-descriptions :column="1" border size="small">
								<el-descriptions-item label="视频格式">{{form.file_format}}</el-descriptions-item>
								<el-descriptions-item label="视频宽高">{{form.width}} (宽) x {{form.height}}
									(高)</el-descriptions-item>
								<el-descriptions-item label="视频时长">{{(form.duration/60).toFixed(2)}}
									分钟</el-descriptions-item>
								<el-descriptions-item label="视频大小">{{(form.file_size/1000/1000).toFixed(2)}}
									MB</el-descriptions-item>
							</el-descriptions>
						</template>
					</el-form-item>

					<div style="height:400px">
						&nbsp;
					</div>
				</el-form>
			</el-main>
			<el-footer>
				<el-button type="primary" :loading="isSaveing" @click="submit">保存</el-button>
				<el-button @click="visible=false">取消</el-button>
			</el-footer>
		</el-container>
	</el-drawer>
</template>

<script>
	var _this;

	import {
		defineAsyncComponent
	} from 'vue';
	const scEditor = defineAsyncComponent(() => import('@/components/scEditor'));
	import scVideo from '@/components/scVideo'

	export default {
		emits: ['success', 'closed'],
		components: {
			scEditor,
			scVideo
		},
		data() {
			return {
				rules: {
					title: [{
						required: true,
						message: '文件名必填'
					}]
				},
				loading: false,
				mode: "add",
				titleMap: {
					add: '上传素材',
					edit: '编辑素材'
				},
				form: {
					id: '',

				},
				rules: {

				},
				visible: false,
				isSaveing: false,
				apiObj: null,
			}
		},
		mounted() {
			_this = this;
		},
		methods: {
			getNextCronTime(str) {
				// return getNextCronTime(str);
			},
			success(res) {
				return true;
			},
			//显示
			open(data) {
				if (data) {
					this.mode = data;
				}
				this.visible = true;
				this.$nextTick(() => {
					if (this.mode == 'add') {

					}
				})
				return this;
			},
			//表单提交方法
			submit() {
				this.$refs.ruleForm.validate(async (data) => {
					if (data) {
						var res;
						this.isSaveing = true;
						if (this.mode == 'edit') {
							res = await _this.$API.system.media.update.post(this.form);
						}
						this.isSaveing = false;
						if (res.code == 0) {
							this.$emit('success', data, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						} else {
							this.$alert(res.message, "提示", {
								type: 'error'
							})
						}
					}
				})
			},
			//表单注入数据
			setData(data) {
				this.loading = true

				if (!data.id) {
					if (data.category_id) {
						this.form.category_id = data.category_id
					}
					this.loading = false
					return;
				}

				const params = {
					id: data.id
				}
				setTimeout(async () => {
					var res = await this.$API.system.media.show.get(params)
					this.loading = false
					this.form = res.data
				}, 0)
			}
		}
	}
</script>

<style>
	.custom-empty {
		width: 100%;
		height: 100%;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		background: #8c939d;
		border-radius: 5px;
	}

	.custom-empty i {
		font-size: 30px;
		color: #fff;
	}

	.custom-empty p {
		font-size: 12px;
		font-weight: normal;
		color: #fff;
		margin-top: 10px;
	}

	.class_title {
		padding-left: 18px;
		font-size: 18px;
		background-color: #f4f9ff;
		text-align: left;
		width: 100%;
	}

	.section-title {
		font-size: 14px;
		font-weight: 400;
		border-left: 5px solid var(--el-color-primary);
		padding-left: 10px;
		color: var(--el-text-color-regular);
	}
</style>
